#include <Windows.h>
#include <tchar.h>
#include <locale.h>
#include "resource.h"

int CALLBACK DlgProc(HWND hDlg, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
	static DWORD nAge				= 0;
	static TCHAR szName[MAX_PATH]	= {0};
	static TCHAR szPhone[MAX_PATH]	= {0};
	static TCHAR szAddr[MAX_PATH]	= {0};
	
	switch (nMsg)
	{
		case WM_INITDIALOG:
		{
			SetWindowText(hDlg, _T("Demo0069"));
			UINT nWidth		= GetSystemMetrics(SM_CXSCREEN);
			UINT nHeight	= GetSystemMetrics(SM_CYSCREEN);
			RECT rtWindow;
			GetWindowRect(hDlg, &rtWindow);
			int  nLeft		= (nWidth - (rtWindow.right - rtWindow.left)) / 2;
			int  nRight		= (nHeight - (rtWindow.bottom - rtWindow.top)) / 2;
			SetWindowPos(hDlg, NULL, nLeft, nRight, 0, 0, SWP_NOSIZE);

			break;
		}
		case WM_COMMAND:
		{
			switch (LOWORD(wParam))
			{
				case IDC_BTNOPEN:
				{
					TCHAR szBuffer[MAX_PATH] = {0};
					OPENFILENAME ofn	= {0};
					ofn.lStructSize		= sizeof(ofn);
					ofn.hwndOwner		= hDlg;
					ofn.lpstrFilter		= _T("All File(*.*)\0*.*\0Text File(*.txt)\0*.txt\0");
					ofn.lpstrTitle		= _T("Open File");
					ofn.lpstrFile		= szBuffer;
					ofn.nMaxFile		= MAX_PATH;
					ofn.Flags			= OFN_EXPLORER;
					if (0 != GetOpenFileName(&ofn))
					{
						HANDLE hFile = CreateFile(szBuffer, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL);
						if (INVALID_HANDLE_VALUE != hFile)
						{
							DWORD dwSize = GetFileSize(hFile, NULL);
							char* szContent = new char[dwSize + 1];
							memset(szContent, 0, dwSize + 1);
							DWORD dwRead = 0;
							WORD dwHead = 0;
							ReadFile(hFile, &dwHead, sizeof(dwHead), &dwRead, NULL);
							if (TRUE == ReadFile(hFile, szContent, dwSize, &dwRead, NULL))
							{
								szContent[dwRead] = _T('\0');
								if (dwHead == 0xFFFE || dwHead == 0xFEFF)
								{
									SetDlgItemText(hDlg, IDC_EDTCONTENT, (TCHAR*)szContent);
								} else {
									TCHAR* szDisData = new TCHAR[dwSize + 1];
									_tsetlocale(LC_ALL, _T("chs"));
									mbstowcs(szDisData, szContent, dwSize);
									SetDlgItemText(hDlg, IDC_EDTCONTENT, szDisData);
									delete[] szDisData;	
								}
							}
							delete[] szContent;
							CloseHandle(hFile);

							_tcscat(szBuffer, _T(" is opened"));
							SetDlgItemText(hDlg, IDC_STCPATH, szBuffer);
						}					
					}

					break;
				}
				case IDC_BTNSAVE:
				{
					TCHAR szBuffer[MAX_PATH] = {0};
					OPENFILENAME ofn	= {0};
					ofn.lStructSize		= sizeof(ofn);
					ofn.hwndOwner		= hDlg;
					ofn.Flags			= OFN_EXPLORER;
					ofn.lpstrFilter		= _T("All File(*.*)\0*.*\0Text FILE(*.txt)\0*.txt\0\0");
					ofn.lpstrFile		= szBuffer;
					ofn.nMaxFile		= MAX_PATH;
					if (0 != GetSaveFileName(&ofn))
					{					
						HANDLE hFile = CreateFile(szBuffer, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, 
												  NULL, CREATE_ALWAYS, NULL, NULL);
						if (INVALID_HANDLE_VALUE != hFile)
						{
							DWORD dwSize		= SendDlgItemMessage(hDlg, IDC_EDTCONTENT, WM_GETTEXTLENGTH, 0, 0);					
							TCHAR* szContent	= new TCHAR[dwSize + 1];
							DWORD  dwFileSize	= sizeof(TCHAR) * (dwSize + 1) + sizeof(WORD);
							GetDlgItemText(hDlg, IDC_EDTCONTENT, szContent, dwSize);
							DWORD dwWrite		= 0;
							HANDLE hFileMap		= CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, dwFileSize, NULL);
							LPVOID lpData		= MapViewOfFile(hFileMap, FILE_MAP_WRITE, 0, 0, dwFileSize);
							if (sizeof(TCHAR) == 2)
							{
								WORD wdHead = 0xFEFF;
								memcpy(lpData, (LPVOID)&wdHead, sizeof(wdHead));
							}

							memcpy((LPBYTE)lpData + sizeof(WORD), (LPVOID)szContent, dwFileSize - sizeof(WORD));
							
							delete[] szContent;
							_tcscat(szBuffer, _T(" is Saved"));
							SetDlgItemText(hDlg, IDC_STCPATH, szBuffer);

							UnmapViewOfFile(lpData);
							CloseHandle(hFileMap);
						}
						CloseHandle(hFile);
					}

					break;
				}
					
			}

			break;
		}

		case WM_CLOSE:
		{
			PostQuitMessage(0);
			break;
		}
	}

	return 0;
}

BOOL WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPreInstance, LPTSTR szCmdLine, int nShowCmd)
{
	DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG), NULL, DlgProc);
	return 0;
}